<?php

	$view['name'] = "view_users";
	$view['version'] = "20250919";
	$view['description'] = "Show the users with the domain name, and contact details";
	$view['sql'] = "	select u.domain_uuid, u.user_uuid, d.domain_name, u.username, u.user_status, u.user_enabled, u.add_date, \n";
	$view['sql'] .= "	c.contact_uuid, c.contact_organization, c.contact_name_given ||' '|| c.contact_name_family as contact_name, c.contact_name_given, c.contact_name_family, c.contact_note, \n";
	$view['sql'] .= "	( \n";
	$view['sql'] .= "		select \n";
	$view['sql'] .= "		string_agg(g.group_name, ', ') \n";
	$view['sql'] .= "		from \n";
	$view['sql'] .= "		v_user_groups as ug, \n";
	$view['sql'] .= "		v_groups as g \n";
	$view['sql'] .= "		where \n";
	$view['sql'] .= "		ug.group_uuid = g.group_uuid \n";
	$view['sql'] .= "		and u.user_uuid = ug.user_uuid \n";
	$view['sql'] .= "	) AS group_names, \n";
	$view['sql'] .= "	( \n";
	$view['sql'] .= "		select \n";
	$view['sql'] .= "		string_agg(g.group_uuid::text, ', ') \n";
	$view['sql'] .= "		from \n";
	$view['sql'] .= "		v_user_groups as ug, \n";
	$view['sql'] .= "		v_groups as g \n";
	$view['sql'] .= "		where \n";
	$view['sql'] .= "		ug.group_uuid = g.group_uuid \n";
	$view['sql'] .= "		and u.user_uuid = ug.user_uuid \n";
	$view['sql'] .= "	) AS group_uuids, \n";
	$view['sql'] .= "	( \n";
	$view['sql'] .= "		SELECT group_level \n";
	$view['sql'] .= "		FROM v_user_groups ug, v_groups g \n";
	$view['sql'] .= "		WHERE (ug.group_uuid = g.group_uuid) \n";
	$view['sql'] .= "		AND (u.user_uuid = ug.user_uuid) \n";
	$view['sql'] .= "		ORDER BY group_level DESC \n";
	$view['sql'] .= "		LIMIT 1 \n";
	$view['sql'] .= "	) AS group_level \n";
	$view['sql'] .= "	from v_contacts as c \n";
	$view['sql'] .= "	right join v_users u on u.contact_uuid = c.contact_uuid \n";
	$view['sql'] .= "	inner join v_domains as d on d.domain_uuid = u.domain_uuid \n";
	$view['sql'] .= "	where 1 = 1 \n";
	$view['sql'] .= "	order by u.username asc \n";
